package com.sxbbc.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.ActivityMapping;
import com.sxbbc.api.common.ProductMapping;
import com.sxbbc.api.service.IOrderService;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.ProGroupVo;
import com.sxbbc.common.core.dto.jz.SearchVo;
import com.sxbbc.common.core.entity.jz.AppBanner;
import com.sxbbc.common.core.entity.jz.OrderConfigGroup;
import com.sxbbc.common.core.entity.jz.OrderDetail;
import com.sxbbc.common.core.entity.jz.OrderMain;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.enums.BannerPositionEnums;
import com.sxbbc.common.core.service.IAppBannerService;
import com.sxbbc.common.core.service.IOrderConfigGroupService;
import com.sxbbc.common.core.service.IOrderDetailService;
import com.sxbbc.common.core.service.IOrderMainService;
import com.sxbbc.common.core.service.IProGroupConfigService;
import com.sxbbc.common.core.service.IProSkuDetailService;
import com.sxbbc.common.core.service.IProcMainService;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 团购 控制层
 *
 * @author leehao
 * @date 2019-06-11 19:05
 */
@RestController

public class GroupController {

    @Autowired
    private IAppBannerService appBannerService;
    @Autowired
    private IProGroupConfigService proGroupConfigService;
    @Autowired
    private IOrderService apiOrderService;
    @Autowired
    private IOrderMainService orderMainService;
    @Autowired
    private IOrderConfigGroupService orderConfigGroupService;
    @Autowired
    private IOrderDetailService orderDetailService;
    @Autowired
    private IProSkuDetailService proSkuDetailService;
    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private IProcMainService procMainService;

    /**
     * 查询拼团商品拼团信息
     *
     * @param page    分页
     * @param proCode 商品编号
     * @return 分页数据
     */
    @RequestMapping(value = ProductMapping.QUERY_PRO_GROUP_LIST)
    public RestResponse queryProGroupList(BasePage<ProGroupVo> page, String proCode) {
        IPage<ProGroupVo> pageInfo = apiOrderService.queryProGroupList(page, proCode);
        return GetRest.getSuccess("", pageInfo);
    }

    /**
     * 查询团购成员信息
     *
     * @param groupCode 团购编号
     * @return Map 团购成员信息和差多少人(用于展示)
     */
    @RequestMapping(value = ProductMapping.QUERY_GROUP_CUS_LIST)
    public RestResponse queryGroupCusList(String groupCode) {
        Map<String, Object> map = apiOrderService.queryGroupCusByGroupCode(groupCode, null);
        return GetRest.getSuccess("", map);
    }

    /**
     * 团购商品
     * banner
     * 今日必拼
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = ActivityMapping.SHOW_GROUP_CENTER)
    public RestResponse queryGroupProduct() {
        //banner
        AppBanner banner = new AppBanner(StaticUtils.ICON_POSITION_APP, BannerPositionEnums.GROUP.getId());
        List<AppBanner> bannerList = appBannerService.queryBannerListForApi(banner);
        //今日必拼
        List<ProGroupVo> groupList = proGroupConfigService.queryTodayGroupProduct();
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("banner", bannerList);
        map.put("today",groupList);
        return GetRest.getSuccess("success", map);
    }

    /**
     * 团购商品列表
     * @param page 分页
     * @param search 搜索
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = ActivityMapping.QUERY_GROUP_RECOMMEND)
    public RestResponse queryGroupRecommend(BasePage<ProGroupVo> page, SearchVo search){
        IPage<ProGroupVo> list = proGroupConfigService.queryGroupProductList(page,search);
        return GetRest.getSuccess("success",list);
    }
    
    /**
     * 查询团购成功之后的信息
     * @param payCode 支付编号
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = ProductMapping.GET_GROUP_SUCCESS)
    public RestResponse getGroupSuccess (String payCode, String token) {
    	Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
    	// 通过payCode查询订单
    	QueryWrapper<OrderMain> orderWrapper = new QueryWrapper<OrderMain>();
		orderWrapper.eq("pay_code", payCode);
		OrderMain order = orderMainService.getOne(orderWrapper);
		if (order == null) {
			return GetRest.getFail("数据有误");
		}
		// 根据订单查询对应的团购信息
		QueryWrapper<OrderConfigGroup> groupWrapper = new QueryWrapper<OrderConfigGroup>();
		groupWrapper.eq("order_code", order.getOrderCode());
		OrderConfigGroup configGroup = orderConfigGroupService.getOne(groupWrapper);
		if (configGroup == null) {
			return GetRest.getFail("数据有误");
		}
		List<OrderDetail> details = orderDetailService.listByOrderCode(configGroup.getOrderCode());
		if (details == null || details.size() <= 0) {
			return GetRest.getFail("数据有误");
		}
		ProcMain detail = procMainService.getById(details.get(0).getProId());
		// 根据团购编码查询
		Map<String, Object> map = apiOrderService.queryGroupCusByGroupCode(configGroup.getGroupCode(), cid);
		map.put("title", detail.getProName());
		map.put("img", detail.getProUrl());
    	return GetRest.getSuccess("", map);
    }
    
    /**
     * 查询团购分享后的页面
     * @param orderCode 订单编号
     * @return
     * @author xiaoky
     */
    @RequestMapping(value = ProductMapping.GET_GROUP_SHARE)
    public RestResponse getGroupShare (String orderCode) {
    	OrderMain order = orderMainService.getByOrderCode(orderCode);
		if (order == null) {
			return GetRest.getFail("数据有误");
		}
    	// 根据订单查询对应的团购信息
		QueryWrapper<OrderConfigGroup> groupWrapper = new QueryWrapper<OrderConfigGroup>();
		groupWrapper.eq("order_code", orderCode);
		OrderConfigGroup configGroup = orderConfigGroupService.getOne(groupWrapper);
		if (configGroup == null) {
			return GetRest.getFail("数据有误");
		}
		
		// 根据团购编码查询
		Map<String, Object> map = apiOrderService.queryGroupCusByGroupCode(configGroup.getGroupCode(), null);
		List<OrderDetail> details = orderDetailService.listByOrderCode(orderCode);
		if (details == null || details.size() <= 0) {
			return GetRest.getFail("数据有误");
		}
		
		OrderDetail orderDetail = details.get(0);
		ProSkuDetail detail = proSkuDetailService.getById(orderDetail.getProDetailId());
		map.put("groupNum", configGroup.getGroupNum());
		detail.setProName(orderDetail.getProName());
		detail.setImgUrl(orderDetail.getProUrl());
		map.put("detail", detail);
    	return GetRest.getSuccess("", map);
    }
}
